package com.yoomiito.app.adapter.material

import android.content.Context
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import android.widget.RelativeLayout
import android.widget.TextView
import com.scrat.app.selectorlibrary.activity.ImagePreviewActivity
import com.yoomiito.app.R
import com.yoomiito.app.base.App
import com.yoomiito.app.base.BaseActivity
import com.yoomiito.app.copyClipboard
import com.yoomiito.app.model.MaterialContent
import com.yoomiito.app.model.ViewType
import com.yoomiito.app.model.bean.VideoInfo
import com.yoomiito.app.showToast
import com.yoomiito.app.ui.video.VideoPlayActivity
import com.yoomiito.app.utils.*
import com.yoomiito.app.widget.RoundImageView
import com.yoomiito.app.widget.SpacingDecoration
import com.yoomiito.app.widget.dialog.ShareBottomDialog_1
import de.hdodenhof.circleimageview.CircleImageView
import java.util.ArrayList

class MaterialAdapter(val context: Context,val datas:MutableList<ViewType<MaterialContent>>) : androidx.recyclerview.widget.RecyclerView.Adapter<androidx.recyclerview.widget.RecyclerView.ViewHolder>() {
    var OnShareClickListener:(ViewType<MaterialContent>)->Unit={}

    override fun onCreateViewHolder(p0: ViewGroup, p1: Int): androidx.recyclerview.widget.RecyclerView.ViewHolder =
            when (p1) {
                Type.MaterialType.TextVideo -> {
                    TextVideoHolder(LayoutInflater.from(context).inflate(R.layout.item_article_video, p0, false))
                }
                Type.MaterialType.TextMultiMap -> {
                    TextImagesHolder(LayoutInflater.from(context).inflate(R.layout.item_article_images, p0, false))
                }
                Type.MaterialType.TextSingleImage -> {
                    TextImageHolder(LayoutInflater.from(context).inflate(R.layout.item_article_image, p0, false))
                }
                else -> {
                    TextHolder(LayoutInflater.from(context).inflate(R.layout.item_article_text, p0, false))
                }
            }

    override fun getItemCount(): Int = datas.size

    override fun onBindViewHolder(p0: androidx.recyclerview.widget.RecyclerView.ViewHolder, p1: Int) {
        val type = datas[p1]
        val data = type.t as MaterialContent
        when(type.viewType){
            Type.MaterialType.TextVideo -> {
                val holder = p0 as TextVideoHolder
                holder.articleText.text = data.content
                holder.articleText.setOnClickListener {
                    data.content.copyClipboard(context)
                }
                holder.userNameTv.text = data.auth
                holder.dateTv.text = data.public_time
                holder.shareTv.setOnClickListener {
                    data.content.copyClipboard(context)
                    OnShareClickListener(type)
                }
                holder.videoClick.setOnClickListener {
                    val videoInfo = VideoInfo()
                    videoInfo.url = data.video
                    videoInfo.width = data.width
                    videoInfo.height = data.height
                    VideoPlayActivity.start(context, videoInfo)
                }
                ImageLoader.getInstance().into(context,data.auth_head,holder.userPhotoIv)
                val lp = holder.videoIv.layoutParams as RelativeLayout.LayoutParams
                val width = if (data.width.isEmpty())0 else data.width.toInt()
                val height = if (data.height.isEmpty()) 0 else data.height.toInt()
                when {
                    width>height -> {
                        lp.width = App.mWidthPx-DensityUtil.dp2px(48f)
                        lp.height = lp.width*height/width
                        R.drawable.default_material_horizontal_pic
                    }
                    width<height -> {
                        lp.width = DensityUtil.dp2px(200f)
                        lp.height = lp.width*height/width
                        R.drawable.default_material_rect_pic
                    }
                    else -> {
                        lp.width = DensityUtil.dp2px(200f)
                        lp.height = lp.width
                        R.drawable.default_material_square_pic
                    }
                }
                ImageLoader.getInstance().into(context,data.video_img,holder.videoIv)
                holder.shareTv.setOnClickListener {
                    OnShareClickListener(type)
                }
            }
            Type.MaterialType.TextMultiMap -> {
                val holder = p0 as TextImagesHolder
                holder.rcy.layoutManager = androidx.recyclerview.widget.GridLayoutManager(context, 3)
                if (holder.rcy.itemDecorationCount > 0) {
                    val itemDecoration = holder.rcy.getItemDecorationAt(0)
                    if (itemDecoration == null) {
                        holder.rcy.addItemDecoration(SpacingDecoration(DensityUtil.dp2px(4f),DensityUtil.dp2px(4f),false))
                    }
                } else {
                    holder.rcy.addItemDecoration(SpacingDecoration(DensityUtil.dp2px(4f),DensityUtil.dp2px(4f),false))
                }
                val picAdapter = PicAdapter(data.img)
                holder.rcy.adapter = picAdapter
                picAdapter.setOnItemClickListener { _, _, position ->
                    ImagePreviewActivity.show(context as BaseActivity<*>, 0, data.img as ArrayList<String>, position)
                }
                holder.articleText.text = data.content

                holder.articleText.setOnClickListener {
                    data.content.copyClipboard(context)
                }
                holder.shareTv.setOnClickListener {
                    OnShareClickListener(type)
                }
                holder.userNameTv.text = data.auth
                holder.dateTv.text = data.public_time
                ImageLoader.getInstance().into(context,data.auth_head,holder.userPhotoIv)
            }
            Type.MaterialType.TextSingleImage -> {
                val holder = p0 as TextImageHolder
                holder.articleText.text = data.content
                holder.articleText.setOnClickListener {
                    data.content.copyClipboard(context)
                }
                holder.shareTv.setOnClickListener {
                    OnShareClickListener(type)
                }
                holder.userNameTv.text = data.auth
                holder.dateTv.text = data.public_time
                ImageLoader.getInstance().into(context,data.auth_head,holder.userPhotoIv)
                val lp = holder.pic.layoutParams as LinearLayout.LayoutParams
                with(holder.pic){
                    ImageLoader.getInstance().into(context,data.img[0],this,when {
                        data.width>data.height -> {
                            lp.width = App.mWidthPx-DensityUtil.dp2px(48f)
                            lp.height = lp.width*data.height.toInt()/data.width.toInt()
                            R.drawable.default_material_horizontal_pic
                        }
                        data.width<data.height -> {
                            lp.width = DensityUtil.dp2px(185f)
                            lp.height = lp.width*data.height.toInt()/data.width.toInt()
                            R.drawable.default_material_rect_pic
                        }
                        else -> {
                            lp.width = DensityUtil.dp2px(200f)
                            lp.height = lp.width
                            R.drawable.default_material_square_pic
                        }
                    })
                    setOnClickListener {
                        ImagePreviewActivity.show(context as BaseActivity<*>, 0, data.img as ArrayList<String>, 0)
                    }
                }


            }
            else -> {
                val holder = p0 as TextHolder
                holder.articleText.text = data.content
                holder.articleText.setOnClickListener {
                    data.content.copyClipboard(context)
                }
                holder.shareTv.setOnClickListener {
                    OnShareClickListener(type)
                }
                holder.userNameTv.text = data.auth
                holder.dateTv.text = data.public_time
                ImageLoader.getInstance().into(context,data.auth_head,holder.userPhotoIv,R.drawable.wo_no)
            }
        }
    }

    override fun getItemViewType(position: Int): Int = datas[position].viewType

    class TextVideoHolder(view: View) : androidx.recyclerview.widget.RecyclerView.ViewHolder(view) {
        val userPhotoIv:CircleImageView = itemView.findViewById(R.id.userPhoto)
        val userNameTv:TextView = itemView.findViewById(R.id.userName)
        val dateTv:TextView = itemView.findViewById(R.id.date)
        val articleText:TextView = itemView.findViewById(R.id.articleText)
        val shareTv:TextView = itemView.findViewById(R.id.share)
        val videoIv:RoundImageView = itemView.findViewById(R.id.videoImage)
        val videoClick:RelativeLayout = itemView.findViewById(R.id.videoClick)
    }

    class TextImagesHolder(view: View) : androidx.recyclerview.widget.RecyclerView.ViewHolder(view) {
        val userPhotoIv:CircleImageView = itemView.findViewById(R.id.userPhoto)
        val userNameTv:TextView = itemView.findViewById(R.id.userName)
        val dateTv:TextView = itemView.findViewById(R.id.date)
        val articleText:TextView = itemView.findViewById(R.id.articleText)
        val shareTv:TextView = itemView.findViewById(R.id.share)
        val rcy: androidx.recyclerview.widget.RecyclerView = itemView.findViewById(R.id.rcy)

    }

    class TextImageHolder(view: View) : androidx.recyclerview.widget.RecyclerView.ViewHolder(view) {
        val userPhotoIv:CircleImageView = itemView.findViewById(R.id.userPhoto)
        val userNameTv:TextView = itemView.findViewById(R.id.userName)
        val dateTv:TextView = itemView.findViewById(R.id.date)
        val articleText:TextView = itemView.findViewById(R.id.articleText)
        val shareTv:TextView = itemView.findViewById(R.id.share)
        val pic:RoundImageView = itemView.findViewById(R.id.pic)
    }

    class TextHolder(view: View) : androidx.recyclerview.widget.RecyclerView.ViewHolder(view) {
        val userPhotoIv:CircleImageView = itemView.findViewById(R.id.userPhoto)
        val userNameTv:TextView = itemView.findViewById(R.id.userName)
        val dateTv:TextView = itemView.findViewById(R.id.date)
        val articleText:TextView = itemView.findViewById(R.id.articleText)
        val shareTv:TextView = itemView.findViewById(R.id.share)

    }

}